import os
import random
import codecs
import json
import time
import logging

logging.basicConfig(filename='mygua.log', level=logging.INFO, filemode='a',
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)


gua_dict = {
    "111": "乾",
    "110": "兑",
    "101": "离",
    "100": "震",
    "011": "巽",
    "010": "坎",
    "001": "艮",
    "000": "坤",
    "111111": {"卦":"乾", "上": "乾", "下": "乾"},
    "000000": {"卦":"坤", "上": "坤", "下": "坤"},
    "100010": {"卦": "屯", "上": "坎", "下": "震"},
    "010001": {"卦": "蒙", "上": "艮", "下": "坎"},
    "111010": {"卦": "需", "上": "坎", "下": "乾"},
    "010111": {"卦": "讼", "上": "乾", "下": "坎"},
    "010000": {"卦": "师", "上": "坤", "下": "坎"},
    "000010": {"卦": "比", "上": "坎", "下": "坤"},
    "111011": {"卦": "小畜", "上": "巽", "下": "乾"},
    "110111": {"卦": "履", "上": "乾", "下": "兑"},
    "111000": {"卦": "泰", "上": "坤", "下": "乾"},
    "000111": {"卦": "否", "上": "乾", "下": "坤"},
    "101111": {"卦": "同人", "上": "乾", "下": "离"},
    "111101": {"卦": "大有", "上": "离", "下": "乾"},
    "001000": {"卦": "谦", "上": "坤", "下": "艮"},
    "000100": {"卦": "豫", "上": "震", "下": "坤"},
    "100110": {"卦": "随", "上": "兑", "下": "震"},
    "011001": {"卦": "蛊", "上": "艮", "下": "巽"},
    "110000": {"卦": "临", "上": "坤", "下": "兑"},
    "000011": {"卦": "观", "上": "巽", "下": "坤"},
    "100101": {"卦": "噬嗑", "上": "离", "下": "震"},
    "101001": {"卦": "贲", "上": "艮", "下": "离"},
    "000001": {"卦": "剥", "上": "艮", "下": "坤"},
    "100000": {"卦": "复", "上": "坤", "下": "震"},
    "100111": {"卦": "无妄", "上": "乾", "下": "震"},
    "111001": {"卦": "大畜", "上": "艮", "下": "乾"},
    "100001": {"卦": "颐", "上": "艮", "下": "震"},
    "011110": {"卦": "大过", "上": "兑", "下": "巽"},
    "010010": {"卦": "坎", "上": "坎", "下": "坎"},
    "101101": {"卦": "离", "上": "离", "下": "离"},
    "001110": {"卦": "咸", "上": "兑", "下": "艮"},
    "011100": {"卦": "恒", "上": "震", "下": "巽"},
    "001111": {"卦": "遁", "上": "乾", "下": "艮"},
    "111100": {"卦": "大壮", "上": "震", "下": "乾"},
    "000101": {"卦": "晋", "上": "离", "下": "坤"},
    "101000": {"卦": "明夷", "上": "坤", "下": "离"},
    "101011": {"卦": "家人", "上": "巽", "下": "离"},
    "110101": {"卦": "睽", "上": "离", "下": "兑"},
    "001010": {"卦": "蹇", "上": "坎", "下": "艮"},
    "010100": {"卦": "解", "上": "震", "下": "坎"},
    "110001": {"卦": "损", "上": "艮", "下": "兑"},
    "100011": {"卦": "益", "上": "巽", "下": "震"},
    "111110": {"卦": "夬", "上": "兑", "下": "乾"},
    "011111": {"卦": "姤", "上": "乾", "下": "巽"},
    "000110": {"卦": "萃", "上": "兑", "下": "坤"},
    "011000": {"卦": "升", "上": "坤", "下": "巽"},
    "010110": {"卦": "困", "上": "兑", "下": "坎"},
    "011010": {"卦": "井", "上": "坎", "下": "巽"},
    "101110": {"卦": "革", "上": "兑", "下": "离"},
    "011101": {"卦": "鼎", "上": "离", "下": "巽"},
    "100100": {"卦": "震", "上": "震", "下": "震"},
    "001001": {"卦": "艮", "上": "艮", "下": "艮"},
    "001011": {"卦": "渐", "上": "巽", "下": "艮"},
    "110100": {"卦": "归妹", "上": "震", "下": "兑"},
    "101100": {"卦": "丰", "上": "震", "下": "离"},
    "001101": {"卦": "旅", "上": "离", "下": "艮"},
    "011011": {"卦": "巽", "上": "巽", "下": "巽"},
    "110110": {"卦": "兑", "上": "兑", "下": "兑"},
    "010011": {"卦": "涣", "上": "巽", "下": "坎"},
    "110010": {"卦": "节", "上": "坎", "下": "兑"},
    "110011": {"卦": "中孚", "上": "巽", "下": "兑"},
    "001100": {"卦": "小过", "上": "震", "下": "艮"},
    "101010": {"卦": "既济", "上": "坎", "下": "离"},
    "010101": {"卦": "未济", "上": "离", "下": "坎"}
}
def get_gua(gua):
    #gua_dict = json.load(codecs.open('guaxiang.json', encoding="utf-8"))
    if gua in gua_dict:
        return gua_dict[gua]["卦"]
    else:
        print("%s not in dict", gua)
        print("上卦:%s， 下卦:%s" % (gua_dict[gua[3:]], gua_dict[gua[:3]]))


def hugua(gua):
    shanggua = gua[2:-1]
    xiagua = gua[1:-2]
    new_gua = xiagua + shanggua
    return new_gua


def biangua(yaos):
    gua = ""
    for yao in yaos:
        if yao == 6:
            gua += "1"
        elif yao == 8:
            gua += "0"
        elif yao == 9:
            gua += "0"
        else:  # 7
            gua += "1"
    return gua


def cuogua(gua):
    new_gua = ""
    for yao in gua:
        if yao == "1":
            new_gua += "0"
        else:
            new_gua += "1"
    return new_gua


def zonggua(gua: str):
    return gua[::-1]


def get_numbers(prompt: str = None):
    now = time.time()
    if prompt is not None:
        prompt = prompt.strip()
        now += hash(prompt)
    random.seed(now)
    yaos = random.choices([6, 7, 8, 9], weights=[1, 3, 3, 1], k=6)
    return yaos


def bengua(yaos):
    gua = ""
    for yao in yaos:
        if yao == 6 or yao == 8:
            gua += "0"
        else:
            gua += "1"
    return gua


def get_bianyao(yaos):
    bianyao = []
    for id, yao in enumerate(yaos):
        if yao in [6, 9]:
            bianyao.append(id+1)
    return bianyao


if __name__ == "__main__":
    question = input("请输入所占之事")
    yaos = get_numbers(question)
    ben_gua = bengua(yaos)
    bianyao = get_bianyao(yaos)
    cuo_gua = cuogua(ben_gua)
    bian_gua = biangua(yaos)
    hu_gua = hugua(ben_gua)
    zong_gua = zonggua(ben_gua)
    import datetime

    current_date = datetime.date.today()
    formatted_date = current_date.strftime("%Y - %m - %d")
    print("本卦：%s\n变爻：%s\n变卦：%s\n错卦：%s\n互卦：%s\n综卦：%s\n" % (
        get_gua(ben_gua), bianyao, get_gua(bian_gua), get_gua(cuo_gua), get_gua(hu_gua), get_gua(zong_gua)))
    '''
    logger.info("本卦：%s\n变爻：%s\n变卦：%s\n错卦：%s\n互卦：%s\n综卦：%s\n" % (
        ben_gua, bianyao, bian_gua, cuo_gua, hu_gua, zong_gua))
    '''
    logger.info(formatted_date)
    logger.info("所问之事：%s"%question)
    logger.info("本卦：%s\n变爻：%s\n变卦：%s\n错卦：%s\n互卦：%s\n综卦：%s\n" % (
        get_gua(ben_gua), bianyao, get_gua(bian_gua), get_gua(cuo_gua), get_gua(hu_gua), get_gua(zong_gua)))
    os.system("pause")
